<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>
    
    <script>
        let methods = {
            isOdd: function(n) {
                return n % 2 == 0;
            },
            isEven: function(n) {
                return n % 2 != 0;
            }
        }

        // 解构作为输入参数
        function sum(n,{isOdd}) {
            let sum = 0;
            for (i = 1;i <= n;i ++) {
                if (isOdd(i)) {
                    sum += i;
                }
            }
            return sum;
        }

        let result = sum(100,methods);
        console.log("result = ",result);

        // 函数返回对象，采用解构赋值，可以同时获取多个结果
        function sum2(n,{isOdd,isEven}) {
            let odd = 0;
            let even = 0;
            for (i = 1;i <= n;i ++) {
                if (isOdd(i)) {
                    odd += i;
                }
                if (isEven(i)) {
                    even += i;
                }
            }
            return {odd:odd,even: even};
        }

        let {odd,even} = sum2(100,methods);
        console.log("odd = ",odd);
        console.log("even = ",even);


    </script>

</body>
</html>